package com.jingxing.controller;

import com.jingxing.config.ApiResult;
import com.jingxing.config.UserLoginToken;
import com.jingxing.pojo.PlayerUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/playerUserInfo")
@Api(value = "API", tags = {"选手相关"})
@UserLoginToken
public class PlayerUserInfoController {
    @Autowired
    private RestTemplate restTemplate;

    private static final String URL_20001 = "http://JINGXINGPROJECT-SERVER";

    @PostMapping("/savePlayerUserInfo")
    @ApiOperation("添加选手")
    @Transactional
    public ApiResult savePlayerUserInfo(PlayerUserInfo playerUserInfo) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("playerUserInfo", playerUserInfo);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/savePlayerUserInfo", requestEntity, ApiResult.class);
    }

    @PostMapping("/getPlayerUserInfos")
    @ApiOperation("获取选手信息")
    public ApiResult getPlayerUserInfos(String name, String phone) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("name", name);
        requestEntity.add("phone", phone);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/getPlayerUserInfos", requestEntity, ApiResult.class);
    }

    @PostMapping("/getPlayerUserInfoById")
    @ApiOperation("通过id获取选手信息")
    public ApiResult getPlayerUserInfoById(String id) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/getPlayerUserInfoById", requestEntity, ApiResult.class);
    }

    @PostMapping("/score")
    @ApiOperation("选手打分")
    public ApiResult score(String id, Integer rating, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("rating", rating);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/score", requestEntity, ApiResult.class);
    }

    @PostMapping("/sign")
    @ApiOperation("选手签约")
    public ApiResult sign(String id, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/sign", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionPlayer")
    @ApiOperation("选手晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayer(String id, Integer promotionStatus, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("promotionStatus", promotionStatus);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/promotionPlayer", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionPlayers")
    @ApiOperation("选手批量晋级/淘汰")
    @Transactional
    public ApiResult promotionPlayers(List<String> ids, Integer promotionStatus, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("ids", ids);
        requestEntity.add("promotionStatus", promotionStatus);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/promotionPlayers", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionSuperPlayer")
    @ApiOperation("新秀晋级")
    @Transactional
    public ApiResult promotionSuperPlayer(String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/promotionSuperPlayer", requestEntity, ApiResult.class);
    }

    @PostMapping("/promotionRepechage")
    @ApiOperation("淘汰选手晋级复活赛")
    public ApiResult promotionRepechage(String id, String updateBy) {
        MultiValueMap<String, Object> requestEntity = new LinkedMultiValueMap<>();
        requestEntity.add("id", id);
        requestEntity.add("updateBy", updateBy);
        return restTemplate.postForObject(URL_20001 + "/playerUserInfo/promotionRepechage", requestEntity, ApiResult.class);
    }


}
